<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use app\admin\model\Department;//部门模型
use app\admin\model\Position;//职位模型
use app\admin\model\Admin;//员工模型

class Organization extends Auth
{
    //公司部门
    public function department(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            $department = new Department();
            $listdata = $department->getdepartment($res);//所有的数据
            if ($request->isPost() && $request->isAjax()) {
                echo json_encode(array('getdepartment' => $listdata['data'], 'size' => $listdata['count']));
            }
        } else {
            return view('frame');
        }
    }

    //公司部门的添加
    public function departmentadd(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $department = new Department();
        if ($request->isGet()) {
            return view('frameinfo', [
                'str' => 'departmentadd',
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['getframe'])) {
            //获取国家的接口
            $country = $this->managecommon()['countrys'];
            echo json_encode(array('country' => $country, 'infos' => '', 'info' => ''));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] = 'departmentadd') {
            //添加数据
            $departret = $department->addeditdepartment($data);
            $udata["status"] = $departret['status'];
            $udata['msg'] = $departret['msg'];
            echo json_encode($udata);
        }

    }

    //子部门的添加
    public function departmentadds(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $department = new Department();
        if ($request->isGet()) {
            return view('frameinfo', [
                'str' => 'departmentadds',
                'id' => $data['id']
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['getframe'])) {
            //获取国家的接口
            $country = $this->managecommon()['countrys'];
            $infos = getinfo($data['id'], 'department');//添加子部门
            echo json_encode(array('country' => $country, 'infos' => $infos, 'info' => ''));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] = 'departmentadds') {
            //添加数据
            $departret = $department->addeditdepartment($data);
            $udata["status"] = $departret['status'];
            $udata['msg'] = $departret['msg'];
            echo json_encode($udata);
        }
    }

    //公司部门的编辑
    public function departmentedit(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $department = new Department();
        if ($request->isGet()) {
            $country_id = getinfo($data['id'], 'department')['country_id'];
            return view('frameinfo', [
                'str' => 'departmentedit',
                'country_id' => $country_id,
                'id' => $data['id']
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['getframe'])) {
            //获取国家的接口
            $country = $this->managecommon()['countrys'];
            $where['id'] = $data['id'];
            $info = getlanginfo('department', $where);
            //判断编辑的部门是否含有子部门
            $exist = Db::name('department')->where('pid', $data['id'])->select();
            if ($exist) {
                $info['isexist'] = '1';
            } else {
                $info['isexist'] = '2';
            }
            echo json_encode(array('country' => $country, 'infos' => '', 'info' => $info));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] = 'departmentedit') {
            //添加数据
            $departret = $department->addeditdepartment($data);
            $udata["status"] = $departret['status'];
            $udata['msg'] = $departret['msg'];
            echo json_encode($udata);
        }
    }

    //部门的删除
    public function departmentdelete(Request $request)
    {
        $department = new Department();
        $data = $request->param();
        $data['lang'] = $this->lang;
        if ($request->isPost() && $request->isAjax()) {
            $departret = $department->deldepartment($data);
            $udata["status"] = $departret['status'];
            $udata['msg'] = $departret['msg'];
            echo json_encode($udata);
        }
    }

    //职务列表
    public function position(Request $request)
    {
        if ($request->isPost() && $request->isAjax()) {
            $position = new Position();
            $res = $request->param();
            if (isset($res['page']) && $res['page']) {
                $page = $res['page'];
            } else {
                $page = 1;
            }
            $pageSize = $res['pageSize'];
            $listdata = $position->getposition($res);//所有的数据
            $getposition = array_slice($listdata['data'], ($page - 1) * $pageSize, $pageSize);
            if ($request->isPost() && $request->isAjax()) {
                foreach ($getposition as $getpositionkey => $getpositionval) {
                    $getposition[$getpositionkey]['level'] = str_repeat('&emsp;', $getpositionval['level'] * 1);
                }
                echo json_encode(array('getposition' => $getposition, 'size' => $listdata['count']));
            }
        } else {
            return view('position/position');
        }
    }

    //职务的添加
    public function positionadd(Request $request)
    {
        $data = $request->param();
        $data['lang'] = $this->lang;
        $position = new Position();
        if ($request->isGet()) {
            return view('position/positioninfo', [
                'str' => 'positionadd',
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['getpostion'])) {
            $country = $this->managecommon()['countrys'];
            echo json_encode(array('country' => $country, 'info' => ''));
        } elseif ($request->isPost() && $request->isAjax() && $data['act'] == 'positionadd') {
            $positionret = $position->addeditposition($data);
            $udata["status"] = $positionret['status'];
            $udata['msg'] = $positionret['msg'];
            echo json_encode($udata);
        }
    }

    //子职位的添加
    public function positionadds(Request $request)
    {
        $position = new Position();
        $data = request()->param();
        $data['lang'] = $this->lang;
        if ($request->isGet()) {
            return view('position/positioninfo', [
                'str' => 'positionadds',
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getpostion'])) {
            $country = $this->managecommon()['countrys'];
            $infos = getinfo($data['id'], 'position');//添加子职位
            echo json_encode(array('country' => $country, 'infos' => $infos));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] = 'positionadds') {
            $positionret = $position->addeditposition($data);
            $udata["status"] = $positionret['status'];
            $udata['msg'] = $positionret['msg'];
            echo json_encode($udata);
        }
    }

    //职务的编辑
    public function positionedit(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $position = new Position();
        if ($request->isGet()) {
            //编辑职务的国家id
            $country_id = getinfo($data['id'], 'position')['country_id'];
            return view('position/positioninfo', [
                'str' => 'positionedit',
                'country_id' => $country_id,
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getpostion'])) {
            //获取国家的接口
            $country = $this->managecommon()['countrys'];
            $where['id'] = $data['id'];
            $info = getlanginfo('position', $where);
            //判断编辑的职位中是否含有子职位
            $exist = Db::name('position')->where('pid', $data['id'])->select();
            if ($exist) {
                $info['isexist'] = '1';
            } else {
                $info['isexist'] = '2';
            }
            echo json_encode(array('country' => $country, 'info' => $info));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'positionedit') {
            $positionret = $position->addeditposition($data);
            $udata["status"] = $positionret['status'];
            $udata['msg'] = $positionret['msg'];
            echo json_encode($udata);
        }
    }

    //职位的复制
    public function positioncopy(Request $request)
    {
        $position = new Position();
        $data = request()->param();
        $data['lang'] = $this->lang;
        if ($request->isGet()) {
            return view('position/positioninfo', [
                'id' => $data['id'],
                'str' => 'positioncopy',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getpostion'])) {
            $country = $this->managecommon()['countrys'];
            $infocopy = getinfo($data['id'], 'position');//复制子职位
            echo json_encode(array('country' => $country, 'infocopy' => $infocopy));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] = 'positioncopy') {
            $positionret = $position->addeditposition($data);
            $udata["status"] = $positionret['status'];
            $udata['msg'] = $positionret['msg'];
            echo json_encode($udata);
        }
    }

    //职务的权限设置
    public function positionroot(Request $request)
    {
        $data = $request->param();
        $data['lang'] = $this->lang;
        $position = new Position();
        if ($request->isGet()) {
            return view('position/setrootinfo', [
                'id' => $data['id'],
                'str' => 'positionroot'
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getposition'])) {
            //语言标志
            $lang = $this->lang;
            $menu = getTreeChild('menu', ['disable' => 0], $lang);//菜单权限显示
            $info = getinfo($data['id'], 'position')['auth_ids'];//职位拥有的权限
            $posits = getpostion('position', $data['id']);//当前职位的父级id
            $positarr = array();
            foreach ($posits as $positkey => $positval) {
                $poids = 'position_' . $positval;
                array_unshift($positarr, $poids);
            }
            $mapwhere['mkey'] = ['in', $positarr];
            //获取对应的职位名称
            $postionname = getlangname('multi_lang', $mapwhere, $lang);
            echo json_encode(array('menu' => $menu, 'info' => $info, 'postionname' => $postionname));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'positionroot') {
            //数据的提交
            $positionret = $position->positionroot($data);
            $udata["status"] = $positionret['status'];
            $udata['msg'] = $positionret['msg'];
            echo json_encode($udata);
        }
    }

    //职务的删除
    public function positiondelete(Request $request)
    {
        $position = new Position();
        $data = $request->param();
        $data['lang'] = $this->lang;
        if ($request->isPost() && $request->isAjax()) {
            $positionret = $position->delposition($data);
            $udata["status"] = $positionret['status'];
            $udata['msg'] = $positionret['msg'];
            echo json_encode($udata);
        }
    }

    //员工列表
    public function personnel(Request $request)
    {
        $res = $request->param();
        $admindata = session('admin');
        if ($request->isAjax() && $request->isPost()) {
            $lang = $res['lang'] = $this->lang;//语言标志
            //获取用户的部门的国家id
            $depar_id = getinfo($admindata['department_id'], 'department')['country_id'];
            //对应国家下部门名称
            $departments = getFrames('department', $lang, $depar_id);

            //职位下对应的国家id
            $posion_id = getinfo($admindata['position_id'], 'position')['country_id'];
            //对应国家下职位名称
            $positions = getFrames('position', $lang, $posion_id);

            $country = $this->managecommon()['countrys'];//国家
            $admin = new Admin();
            $getadmin = $admin->getadmin($res);
            echo json_encode(array('getmanage' => $getadmin['data'], 'size' => $getadmin['count'], 'dataids' => $getadmin['ids'], 'frames' => $departments, 'roles' => $positions, 'country' => $country, 'lang' => $lang));
        } else if ($request->isGet()) {
            return view('admin/admin');
        }
    }

    //员工的详情查看
    public function personnelxiangqin(Request $request)
    {
        $res = $request->param();
        if ($request->isGet()) {
            $res['lang'] = $this->lang;//语言标志
            $admin = new Admin();
            $xiangqindata = $admin->getxiangqin($res);
            return view('admin/adminxq', ['xqdata' => $xiangqindata]);
        }
    }

    //处理员工的锁定与批量锁定
    public function personnelock(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isPost() && $request->isAjax()) {
            $adminret = $admin->lockleaveresume($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //处理员工的解锁
    public function personnelunlock(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isPost() && $request->isAjax()) {
            $adminret = $admin->lockleaveresume($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //处理员工的离职
    public function personneleave(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isPost() && $request->isAjax()) {
            $adminret = $admin->lockleaveresume($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //处理员工的复职
    public function personneresume(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isPost() && $request->isAjax()) {
            $adminret = $admin->lockleaveresume($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //处理员工的清除
    public function personnedelete(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isPost() && $request->isAjax()) {
            $adminret = $admin->lockleaveresume($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //处理员工的重置密码
    public function personnelreset(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        $adminret = $admin->resetadmin($data);
        return view('admin/resetpass', ['reset' => $adminret]);
    }

    //员工的添加
    public function personneladd(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        $langs = $data['lang'] = $this->lang;
        if ($request->isGet()) {
            $year = getTimeDate(null, 1) - 20;//当前时间减去20年
            $adddata = $this->managecommon();
            //入职时间
            if ($langs != 'zh') {
                $yeardate = '01-01-' . $year;//m-d-Y
                $work_day = getTimeDate(null, 13);//m-d-Y
            } else {
                $yeardate = $year . '-01-01';//Y-m-d
                $work_day = getTimeDate(null, 7);//Y-m-d
            }
            return view('admin/admininfo', [
                'work_day' => $work_day,//入职时间
                'birth' => $yeardate,//生日
                'str' => 'personneladd',
                'lang' => $adddata['lang'],//语言
                'status' => $adddata['status'],//员工状态
                'sex' => $adddata['sex'],//性别
                'education' => $adddata['education'],//学历
                'marry' => $adddata['marry'],//是否单身
                'countrys' => $adddata['countrys'],//国家
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'personneladd') {
            $adminret = $admin->addeditadmin($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            $udata['name'] = isset($adminret['name']) ? $adminret['name'] : '';
            $udata['passwd'] = isset($adminret['passwd']) ? $adminret['passwd'] : '';
            echo json_encode($udata);
        }
    }

    //员工的编辑
    public function personneledit(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        $data['lang'] = $this->lang;
        if ($request->isGet()) {
            $adddata = $this->managecommon();
            $info = Db::name('admin')->alias('a')->join('nt_admin_info b', 'a.id=b.admin_id', 'left')->field('a.*,b.firstname,b.lastname,b.birthday,b.remarks,b.sex,b.marry,b.wage,b.education')->where('a.id', $data['id'])->find();
            if ($data['lang'] != 'zh') {
                $info['work_day'] = getTimeDate($info['work_day'], 8);//入职日期
                $info['birthday'] = getTimeDate($info['birthday'], 8);//生日
                if ($info['leave_day']) {
                    $info['leave_day'] = getTimeDate($info['leave_day'], 8);//离职日期
                }
            } else {
                $info['work_day'] = getTimeDate($info['work_day'], 7);//入职日期
                $info['birthday'] = getTimeDate($info['birthday'], 7);//生日
                if ($info['leave_day']) {
                    $info['leave_day'] = getTimeDate($info['leave_day'], 7);//离职日期
                }
            }
            //部门的国家id和职位国家id
            $info['decountry_id'] = getinfo($info['department_id'], 'department')['country_id'];
            $info['pocountry_id'] = getinfo($info['position_id'], 'position')['country_id'];
            //判断日期格式
            return view('admin/admininfo', [
                'str' => 'personneledit',
                'info' => $info,//编辑的信息
                'lang' => $adddata['lang'],//语言
                'status' => $adddata['status'],//在职状态
                'sex' => $adddata['sex'],//性别
                'education' => $adddata['education'],//学历
                'marry' => $adddata['marry'],//是否结婚
                'countrys' => $adddata['countrys'],//国家
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'personneledit') {
            $adminret = $admin->addeditadmin($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }

    }

    //员工的权限的设置
    public function persetroot(Request $request)
    {
        $data = $request->param();
        $admin = new Admin();
        if ($request->isGet()) {
            return view('admin/setroot', [
                'id' => $data['id'],
                'str' => 'persetroot'
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getroot'])) {
            $info = getinfo($data['id'], 'admin');
            $datainfo['nickname'] = $info['nickname'];//昵称
            $lang = $this->lang;
            //权限设置的接口
            $rolewhere['mkey'] = 'position_' . $info['position_id'];
            $datainfo['posname'] = getlangname('multi_lang', $rolewhere, $lang);//职务名称

            $framewhere['mkey'] = 'department_' . $info['department_id'];
            $datainfo['framname'] = getlangname('multi_lang', $framewhere, $lang);//部门名称

            $datainfo['auth_ids'] = $info['auth_ids'];
            $datainfo['menu'] = getTreeChild('menu', ['disable' => 0], $lang);//菜单权限显示
            echo json_encode($datainfo);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'persetroot') {
            $adminret = $admin->setroot($data);
            $udata["status"] = $adminret['status'];
            $udata['msg'] = $adminret['msg'];
            echo json_encode($udata);
        }
    }

    //员工添加与编辑公用数据
    protected function managecommon()
    {
        //在职状态
        $status = array(
            '1' => lang("on_the_job"),
            '2' => lang("departure"),
            '3' => lang("locking"),
        );
        //学历
        $education = array(
            '1' => lang("unknown"),
            '2' => lang("undergraduate"),
            '3' => lang("Master_degree"),
            '4' => lang("Doctor"),
            '5' => lang("college_degree"),
            '6' => lang("technical_secondary_school"),
            '7' => lang("below_high_school"),
        );
        //性别
        $sex = array(
            '1' => lang("man"),
            '2' => lang("woman"),
        );
        //是否结婚
        $marry = array(
            '1' => lang("married"),
            '2' => lang("unmarried")
        );
        //语言
        $langname = array(
            '1' => lang("Chinese"),
            '2' => lang("English"),
            '3' => lang("Portuguese")
        );
        $countrys = array(
            '0' => lang("global"),
            '1' => lang("BRAZIL"),
            '2' => lang("CHINA")
        );//国家
        return array('status' => $status, 'education' => $education, 'sex' => $sex, 'marry' => $marry, 'lang' => $langname, 'countrys' => $countrys);
    }
}